package vip.source.pattern.part06.improve;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import vip.source.pattern.part06.base.domain.CreateAccountMq;
import vip.source.pattern.part06.base.domain.InnerOrderMq;
import vip.source.pattern.part06.improve.adapter.MQAdapter;
import vip.source.pattern.part06.improve.domain.RebateInfo;
import vip.source.pattern.part06.improve.service.OrderAdapterService;
import vip.source.pattern.part06.improve.service.impl.ExternalAdapterServiceImpl;
import vip.source.pattern.part06.improve.service.impl.InnerAdapterOrderServiceImpl;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/** @Description: 适配器模式测试 @Author: zy */
public class UnitTest {
    @Test
    public void test_MQAdapter()
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
                    ParseException {

        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = s.parse("2020-06-01 23:20:16");

        CreateAccountMq createAccount = new CreateAccountMq();
        createAccount.setNumber("100001");
        createAccount.setAddress("河北省.廊坊市.广阳区.大学里职业技术学院");
        createAccount.setAccountDate(parse);
        createAccount.setDesc("在校开户");

        HashMap<String, String> link01 = new HashMap<String, String>();
        link01.put("userId", "number");
        link01.put("bizId", "number");
        link01.put("bizTime", "accountDate");
        link01.put("desc", "desc");
        RebateInfo rebateInfo01 = MQAdapter.filter(createAccount.toString(), link01);
        System.out.println("mq.createAccount(适配前)" + createAccount.toString());
        System.out.println("mq.createAccount(适配后)" + JSON.toJSONString(rebateInfo01));

        System.out.println("");

        InnerOrderMq innerOrderMq = new InnerOrderMq();
        innerOrderMq.setUid("100001");
        innerOrderMq.setSku("10928092093111123");
        innerOrderMq.setOrderId("100000890193847111");
        innerOrderMq.setCreateOrderTime(parse);

        HashMap<String, String> link02 = new HashMap<String, String>();
        link02.put("userId", "uid");
        link02.put("bizId", "orderId");
        link02.put("bizTime", "createOrderTime");
        RebateInfo rebateInfo02 = MQAdapter.filter(innerOrderMq.toString(), link02);
        System.out.println("mq.innerOrderMq(适配前)" + innerOrderMq.toString());
        System.out.println("mq.innerOrderMq(适配后)" + JSON.toJSONString(rebateInfo02));
    }

    @Test
    public void test_itfAdapter() {
        OrderAdapterService popOrderAdapterService = new ExternalAdapterServiceImpl();
        System.out.println("判断首单，接口适配(POP)：" + popOrderAdapterService.isFirst("100001"));

        OrderAdapterService insideOrderService = new InnerAdapterOrderServiceImpl();
        System.out.println("判断首单，接口适配(自营)：" + insideOrderService.isFirst("100001"));
    }
}
